/**
 * 用户统计列表
 */
var userStatList = function() {
    var self = this;
    PageList.call(self);

    self.table = null;
    self.userInput = null;
    self.gameSelect = null;
    self.startInput = null;
    self.endInput = null;
    self.minInput = null;
    self.maxInput = null;
    self.refereeInput = null;
    self.refreshBtn = null;
};
userStatList.prototype = Object.create(PageList.prototype);

userStatList.prototype.init = function() {
    var self = this;

    // 填充游戏
    if (self.gameSelect) {
        var games = get_game_list();
        var l = '<option value="__v__">__name__</option>';
        for (var i in games) {
            var option = l.replace(/__v__/, games[i]);
            option = option.replace(/__name__/, get_game_display_name(games[i]));
            self.gameSelect.append(option);
        }
    }

    if (self.startInput) self.startInput.datepicker({
        format: 'yyyy-mm-dd'
    });
    if (self.endInput) self.endInput.datepicker({
        format: 'yyyy-mm-dd'
    });
    
    if (self.refreshBtn) self.refreshBtn.click(function() {
        self.refresh(1);
    });
    var t = new Date().Format("yyyy-MM-dd");
    if (self.startInput) self.startInput.datepicker("setValue", t);
    if (self.endInput) self.endInput.datepicker("setValue", t);
        
    self.jumpPage = function(page) {
        self.refresh(page);
    };
    self.refresh(1);
}

userStatList.prototype.refresh = function(page) {
    var self = this;
    self.currPage = page;
    var uid = undefined;
    if (self.userInput) {
        uid = self.userInput.val().trim();
        if (uid == '') uid = undefined;
    }

    var start, end;
    if (self.startInput) start = self.startInput.val();
    if (self.endInput) end = self.endInput.val();

    var type = -1;
    if (self.gameSelect) type = self.gameSelect.val();
    if (type == -1) type = undefined;

    var min = undefined, max = undefined;
    if (self.minInput) min = self.minInput.val().trim();
    if (self.maxInput) max = self.maxInput.val().trim();
    if (min == '') min = undefined;
    if (max == '') max = undefined;

    var referee = undefined;
    if (self.refereeInput) referee = self.refereeInput.val().trim();

    // 检索数据
    startLoading();
    CMD.stat_user_squery(type, referee, uid, start, end, min, max, 
                        self.isDaiLiBaoBiao, self.getPageSize(), page, function(r) {
        stopLoading();
        self.list = r;
        self.fillResults(r, r.sum);
    });
}

userStatList.prototype.fillResults = function(list, sum) {
    var self = this;
    self.table.empty();
    self.fillPages(list.count, list.number, list.page, list.size);

    var type = self.gameSelect ? self.gameSelect.val() : -1;

    // 合计
    var d = sum;
    var tr = $("<tr></tr>");
    tr.append("<td colspan='5'>合计</td>");
    tr.append("<td>" + d.depositSum/100 + "</td>");
    tr.append("<td>" + d.withdrawSum/100 + "</td>");
    tr.append("<td>" + d.rewardSum/100 + "</td>");
    tr.append("<td>" + d.penaltySum/100 + "</td>");
    tr.append("<td>" + d.depositCount + "/" + d.withdrawCount + "</td>");
    tr.append("<td>" + d.betSum/100 + "</td>");
    tr.append("<td>" + d.betCount + "</td>");
    tr.append("<td>" + d.betWin/100 + "</td>");
    tr.append("<td>" + d.betProfit/100 + "</td>");
    tr.append("<td>" + d.betBack/100 + "</td>");
    tr.append("<td>" + (d.betProfit - d.betBack)/100 + "</td>");
    tr.append("<td colspan='2'></td>");
    self.table.append(tr);
    
    var list = list.list;
    for (var i in list) {
        var d = list[i];
        var user = d.user;

        var win = 0;
        var tr = $("<tr></tr>");
        var name = '<span class="gray">' + user.mobile + '</span>';
        if (d.online == 1)
            name = '<span class="blue">' + user.mobile + '</span>';
        tr.append("<td class='name'>" + name + "</td>");

        if (self.isDaiLi) {
            tr.append("<td>" + (user.name || '') + "</td>");
            tr.append("<td>" + (d.referee || 0) + "</td>");
            tr.append("<td>" + (user.balance || 0)/100 + "</td>");
            tr.append("<td>" + new Date(user.register).Format("yyyy-MM-dd") + "</td>");
            tr.append("<td>" + new Date(user.login).Format("yyyy-MM-dd") + "</td>");
            self.table.append(tr);
            continue;
        }

        if (d.type == '')
            tr.append("<td>" + "全部游戏" + "</td>");
        else
            tr.append("<td>" + get_game_display_name(d.type) + "</td>");
                    
        tr.append("<td>" + (user.name || '') + "</td>");
        
        if (self.isDaiLiBaoBiao)
            tr.append("<td>" + (d.referee || 0) + "</td>");
        if (self.isHuiYuanBaoBiao)
            tr.append("<td>" + (user.referee || '') + "</td>");

        tr.append("<td>" + ((user.balance || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + ((d.depositSum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + ((d.withdrawSum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + ((d.rewardSum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + ((d.penaltySum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + (d.depositCount || 0) + "/" + (d.withdrawCount || 0) + "</td>");

        if (type == -1) {
            // 所有游戏
            tr.append("<td>" + ((d.betSum || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.betCount || 0)) + "</td>");
            tr.append("<td>" + ((d.betWin || 0)/100).toFixed(3) + "</td>");
            tr.append("<td>" + ((d.betProfit || 0)/100).toFixed(3) + "</td>");
            tr.append("<td>" + ((d.backSum || 0)/100).toFixed(2) + "</td>");
            win = (d.betProfit || 0) + (d.backSum || 0);
        }
        else {
            // 某个游戏
            var data = d.bet[type] || {};
            tr.append("<td>" + ((data.sum || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((data.count || 0)) + "</td>");
            tr.append("<td>" + ((data.betWin || 0)/100).toFixed(3) + "</td>");
            tr.append("<td>" + ((data.profit || 0)/100).toFixed(3) + "</td>");
            tr.append("<td>" + ((data.back || 0)/100).toFixed(2) + "</td>");
            win = (data.profit || 0) + (data.back || 0);
        }
        tr.append("<td>" + ((win || 0)/100).toFixed(2) + "</td>");

        tr.append("<td>" + new Date(user.register).Format("yyyy-MM-dd") + "</td>");
        tr.append("<td>" + new Date(user.login).Format("yyyy-MM-dd") + "</td>");
        self.table.append(tr);
    }
}

window.UserStatList = new userStatList();
